<template>
	<view class="page">
		<view class="form">
			<uni-forms ref="form" :rules="rules" :modelValue="formData" labelWidth="80px">
				<uni-forms-item label="姓名" required name="name"> 
					<uni-easyinput v-model="formData.name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="性别" required name="sex">
					<uni-data-checkbox v-model="formData.sex" :localdata="sexs" />
				</uni-forms-item>
				<uni-forms-item label="身份证号" required name="idNumber">
					<uni-easyinput v-model="formData.idNumber" placeholder="请输入身份证号" />
				</uni-forms-item>
				<uni-forms-item label="籍贯" required name="nativePlace">
					<uni-easyinput v-model="formData.nativePlace" placeholder="请输入籍贯" />
				</uni-forms-item>
				<uni-forms-item label="家庭住址" required name="address">
					<uni-easyinput v-model="formData.address" placeholder="请输入家庭住址" />
				</uni-forms-item>
				<uni-forms-item required label="您的身份" name="role">
					<uni-data-picker @change="onchange" v-model="formData.role" :localdata="roleRange" popup-title="选择身份">
					</uni-data-picker>
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit('baseForm')">提交</button>
		</view>
	</view>
</template>

<script>
	import request from "@/utils/api.js"
	
	export default {
		onLoad() {
			let userInfo = uni.getStorageSync('userInfo')
			this.formData.name = userInfo.name
			this.formData.sex = parseInt(userInfo.gender)
			this.formData.nativePlace = userInfo.address
			this.formData.address = userInfo.location
			this.formData.idNumber = userInfo.idCard
			this.formData.role = parseInt(userInfo.type)
			console.log('原信息',this.formData);
		},
		data() {
			return {
				formData: {
					name: '',
					sex: 2,
					idNumber:'',
					nativePlace:'',
					address:'',
					role:0,
					type:''
				},
				roleRange: [{
					text: "在校学生",
					value: 0,
				}, {
					text: "教师职工",
					value: 1,
				}, {
					text: "驻地人员",
					value: 2,
				}, {
					text: "临时来访",
					value: 3,
				}],
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					idNumber:{
						rules: [{
							required: true,
							errorMessage: '身份证号不能为空'
						}]
					},
					nativePlace:{
						rules: [{
							required: true,
							errorMessage: '籍贯不能为空'
						}]
					},
					address:{
						rules: [{
							required: true,
							errorMessage: '地址不能为空'
						}]
					},
					role:{
						rules: [{
							required: true,
							errorMessage: '身份不能为空'
						}]
					}
				},
				// 单选数据源
				sexs: [{
					text: '男',
					value: 0
				}, {
					text: '女',
					value: 1
				}, {
					text: '保密',
					value: 2
				}],
			}
		},
		methods: {
			
			refreshUserInfo() {
				request('/my/info').then(resp => {
					console.log(resp);
					uni.setStorage({
						key: 'userInfo',
						data: resp.user
					})
				})
			},
			onchange(e) {
				const value = e.detail.value
				this.formData.type = value['0'].text
				console.log('选择数据',this.formData.type)
			},
			submit(ref) {
				console.log('表单数据',this.formData);
				let that = this
				this.$refs.form.validate().then(res => {
					uni.hideLoading()
					if(that.formData.type!==''){
						console.log('表单验证成功，表单数据信息为：', res);
					}else{
						uni.showToast({
							icon:'error',
							title:'请选择你的身份'
						});
						return
					}
					request('/my/info',{
						name:that.formData.name,
						gender:that.formData.sex,
						idCard:that.formData.idNumber,
						address:that.formData.nativePlace,
						location:that.formData.address,
						type:that.formData.role
					},'POST').then(res=>{
						console.log('响应信息', res);
						if(res.code!==200){
							uni.showToast({
								icon:'error',
								title:res.msg
							})
							return
						}
						//注册成功将基本信息填入userInfo
						// 刷新用户信息
						that.refreshUserInfo()
						uni.showToast({
							icon:'success',
							title:'更改成功'
						});
						// 注册成功之后跳转页面
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}).catch(err=>{
						console.log('err',err)
					})
					
				}).catch(err => {
					uni.hideLoading()
					console.log('表单验证失败，表单错误信息为：', err);
				})
			},
		}
	}
</script>

<style>
	.form {
		padding: 15px;
		background-color: #fff;
	}
	.uni-list-cell {
	    justify-content: flex-start
	}
</style>
